package com.woniuxy.Uitl;

import com.woniuxy.dto.OrderDTO;
import com.woniuxy.fegin.GoodsFeign;
import com.woniuxy.mapper.MoneyMapper;
import com.woniuxy.mapper.OrderMapper;
import com.work.framework.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author Administrator
 * @title: OrderUtil
 * @projectName mashangpai
 * @description: TODO
 * @date 2019/9/1715:03
 */
public class OrderUtil {
    @Autowired
    private GoodsFeign goodsFeign;

    @Autowired
    private MoneyMapper moneyMapper;

    /**
     * 操作状态判断
     * @param code
     * @return
     */

    public  String state(Integer code){
        String massage = "";
        if (code==200){
            massage ="操作成功";
        }else if (code==500){
            massage = "操作失败";
        }else if (code==501){
            massage="余额不足";
        }
        return massage;
    }


    /**
     * 批量订单时间的判断
     * @param orderDTOS
     * @return
     * @throws ParseException
     */
    public List<OrderDTO> verify(List<OrderDTO> orderDTOS,OrderMapper orderMapper,RedisTemplate redisTemplate ) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long newTime = System.currentTimeMillis();
        for (OrderDTO dto : orderDTOS) {
            if (dto.getFlage() == 1) {
                long buildTime = 0;
                try {
                    buildTime = simpleDateFormat.parse(dto.getBuildTime()).getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                long time = newTime - buildTime;
                //订单是否过期
                if (time > 15 * 60 * 1000) {
                    //订单过期，删除redis中的缓存
                    dto.setFlage(2);
                    //订单过期
                    orderMapper.updateOrder(dto.getOid(),dto.getFlage());
                    //修改竞拍信息
                    Auction auction = moneyMapper.findAuctionById(dto.getGid());
                    goodsFeign.updateStatus(auction.getAid(),6);
                    //删除redis中的订单信息
                    redisTemplate.opsForHash().delete("订单",dto.getOrderNum());
                }
            }
        }
        return orderDTOS;
    }

    public OrderDTO addOrderDTO(String orderNum, String gname,String cname) {
        if (cname==null){
            cname="%%";
        }else {
            cname = "%" + cname + "%";
        }
        if (orderNum==null){
            orderNum="%%";
        }else {
            orderNum = "%" + orderNum + "%";
        }
        if (gname==null){
            gname="%%";
        }else {
            gname = "%" + gname + "%";
        }
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setCname(cname);
        orderDTO.setGname(gname);
        orderDTO.setOrderNum(orderNum);
        return orderDTO;
    }
}
